<?php
if (!defined('_PS_VERSION_'))
	exit;

if (!class_exists('OverrideModule'))
	require_once(dirname(__FILE__) .'/Module.php');

require_once(_PS_MODULE_DIR_.'prestatheme/configs/config.inc.php');

class PrestaTheme extends OverrideModule
{
	private $_html = '';
    public static $CONFIGURATIONVAR = 'PT_';

	public function __construct()
	{
		$this->name = 'prestatheme';
		$this->tab = 'front_office_features';
		$this->version = '2.0';
		$this->author = 'SEOWEBD';
		$this->need_instance = 0;
		$this->secure_key = Tools::encrypt($this->name);
		
		// Min version of PrestaShop wich the module can be install
		$this->ps_versions_compliancy['min'] = '1.5';
		// Max version of PrestaShop wich the module can be install
		$this->ps_versions_compliancy['max'] = '1.6';
		
		// Modules needed for install
		$this->dependencies = array();

		parent::__construct();

		$this->displayName = $this->l('Prestatheme : Prestashop theme editor');
		$this->description = $this->l('Offers the possibility to edit and create a new prestashop theme.');
        
        //configuration
        if (defined('_PS_ADMIN_DIR_'))
            $this->initConfigs();
        
        if ($this->active && Configuration::get(self::$CONFIGURATIONVAR. 'NBR_DOWNLOAD_ALLOWED') == '')
            $this->warning = $this->l('You have to configure the module');
	}
    
    public function getContent()
    {
        /*
        $this->manageTabs();
        */
        $link = new Link();
        $frontLink = $link->getModuleLink('prestatheme', 'myprojects', array(), true);
        
        $adminTabId = Tab::getIdFromClassName('AdminProjects');
		$AdminProjectsToken = Tools::getAdminToken('AdminProjects'.(int)$adminTabId.(int)$this->context->employee->id);
        $AdminProjectsLink = 'index.php?controller=AdminProjects&amp;token='. $AdminProjectsToken;
        
        $adminTabId = Tab::getIdFromClassName('AdminPrestathemeCms');
		$AdminPrestathemeCmsToken = Tools::getAdminToken('AdminPrestathemeCms'.(int)$adminTabId.(int)$this->context->employee->id);
        $AdminPrestathemeCmsLink = 'index.php?controller=AdminPrestathemeCms&amp;token='. $AdminPrestathemeCmsToken;
        
        $output  = '<h2>'. $this->displayName .'</h2>';
        $output .= '
            <fieldset>
                <legend>
                    '. $this->l('About prestatheme module') .'
                </legend>
                
                <p>
                    '. $this->l('Prestatheme is a web application which has two main components: a prestashop module and a php application independent to prestashop.') .'
                </p>
                <p>
                    '. $this->l('This standalone application is indeed the heart of the project prestathème. This is the theme editor.') .'
                </p>
                <p>
                    '. $this->l('The module prestatheme is a prestashop module whose main role is to link the theme editor to prestashop.') .'
                </p>
                <p>
                    '. $this->l('It offers a front office interface that allows users to manage the themes they publish, and a link to start the theme editor to create or modify a theme.') .'
                    '. $this->l('This interface can be accessed from') .' <a href="'. $frontLink .'" class="button" target="_blank">'. $this->l('here') .'</a>
                </p>
                <p>
                    '. $this->l('Back in, it has a management interface themes available') .' <a href="'. $AdminProjectsLink .'" class="button">'. $this->l('here') .'</a> 
                    '. $this->l('and an interface to manage the multi-cms available') .' <a href="'. $AdminPrestathemeCmsLink .'" class="button">'. $this->l('here') .'</a>
                </p>
            </fieldset><br />
                ';
        $output .= parent::getContent();
        return $output;
    }

	private function _prepareHook()
	{
		$publicProjects = array();

		$this->smarty->assign('publicProjects', $publicProjects);
		return true;
	}

	public function hookDisplayHome()
	{
		if(!$this->_prepareHook())
			return;
        
		$this->context->controller->addCSS($this->_path.'views/css/style.css');
		$this->context->controller->addJS($this->_path.'views/js/prestatheme.js');
		return $this->display(__FILE__, 'views/templates/front/prestatheme-home.tpl');
	}
    
    public function hookDisplayCustomerAccount ($params)
    {
		global $smarty;
        $smarty->assign(array(
				'link' => $this->context->link,
            )
		);
		return $this->display(__FILE__, 'views/templates/hook/customer-account.tpl');
    }
    
    public function hookDisplayMyAccountBlock ($params)
    {
		global $smarty;
        $smarty->assign(array(
				'link' => $this->context->link,
            )
		);
		return $this->display(__FILE__, 'views/templates/hook/my-account.tpl');
    }
    
    public function initConfigs ()
    {
		//we initialise the module
		$this->extraConfigsTitle = $this->l('Settings');
        
        $this->extraHooks = array('displayHome', 'displayCustomerAccount', 'displayMyAccountBlock');
        
		$this->extraTables  = array(
			'install' =>  'install.sql',
			'uninstall' => 'uninstall.sql'
		);
		$this->extraFiles   = array(
            'themes/prestashop/img/icon/prestatheme.gif',
            'themes/prestashop/img/icon/pt_add.png',
            'themes/prestashop/img/icon/pt_edit.png',
            'themes/prestashop/img/icon/pt_view.png',
            'themes/prestashop/img/icon/pt_delete.png',
            'themes/prestashop/img/icon/pt_editor.png',
            'themes/prestashop/img/icon/pt_packager.png',
            'administration/ajax_prestatheme_css_style.php',
        );
		
		//manage tabs
		$idDevTab = (int)Tab::getIdFromClassName('AdminSeowebdDev');
		$this->extraTabs = array();
		if (empty($idDevTab))
		{
			$this->installModuleTab('AdminSeowebdDev', 'SEOWEBD Dev', 0, 'seowebd.gif');
			$idDevTab = (int)Tab::getIdFromClassName('AdminSeowebdDev');
		}
		$this->extraTabs['AdminPrestathemeCms']    = array('tabName' => 'Prestatheme CMS', 'idTabParent' => (int)$idDevTab, 'logoName' => 'AdminPrestathemeCms.gif');
		$this->extraTabs['AdminPrestathemeEditor'] = array('tabName' => 'Prestatheme Editor', 'idTabParent' => (int)$idDevTab, 'logoName' => 'AdminPrestathemeEditor.gif');
		$this->extraTabs['AdminProjects']          = array('tabName' => 'Prestatheme Projects', 'idTabParent' => (int)$idDevTab, 'logoName' => 'AdminProjects.gif');
		
        $this->extraTabs['AdminThemeEditor']         = array('tabName' => 'Theme editor', 'idTabParent' => (int)$idDevTab, 'logoName' => 'AdminProjects.gif', 'active' => false);
		$this->extraTabs['AdminConfigTab']           = array('tabName' => 'Tab on Editor', 'idTabParent' => (int)$idDevTab, 'logoName' => 'AdminProjects.gif', 'active' => false);
        $this->extraTabs['AdminConfigItem']          = array('tabName' => 'Item on Editor', 'idTabParent' => (int)$idDevTab, 'logoName' => 'AdminProjects.gif', 'active' => false);
        $this->extraTabs['AdminConfigOption']        = array('tabName' => 'Option on Editor', 'idTabParent' => (int)$idDevTab, 'logoName' => 'AdminProjects.gif', 'active' => false);
        // $this->extraTabs['AdminDefaultStyle']        = array('tabName' => 'Default Style', 'idTabParent' => (int)$idDevTab, 'logoName' => 'AdminProjects.gif', 'active' => false);
        // $this->extraTabs['AdminPreConfiguredDesign'] = array('tabName' => 'Pre-configured design', 'idTabParent' => (int)$idDevTab, 'logoName' => 'AdminProjects.gif', 'active' => false);
        
		$adminTabId = Tab::getIdFromClassName('AdminTaxRulesGroup');
		$adminTaxRulesGroupToken = Tools::getAdminToken('AdminTaxRulesGroup'.(int)$adminTabId.(int)$this->context->employee->id);
        $this->extraConfigs = array(
            array(
                'ATTRIBUTES' => array(
                    'title' => $this->l('Downloadable product configs'),
                    'fieldset_style' => 'float:left;width: 47%;padding: 5px;margin: 5px;min-height:112px;',
                ),
                self::$CONFIGURATIONVAR. 'NBR_DOWNLOAD_ALLOWED' => array(
    				'type'       => 'test', 
    				'title'      => $this->l('Number of downloads allowed'),
                    'default'    => '',
                    'size'       => '30',
                    'validation' => 'isInt',
                    'desc'       => $this->l('Number of downloads allowed per client (set to 0 for unlimited downloads)')
    			),
                self::$CONFIGURATIONVAR. 'EXPIRED_DATE' => array(
    				'type'       => 'date', 
    				'title'      => $this->l('Expiration date'),
                    'default'    => '2035-10-12',
                    'size'       => '30',
                    'validation' => 'isDate',
                    'desc'       => $this->l('If set, the file is no longer available for download from this date. Leave blank if you do not want expiration date')
    			),
                self::$CONFIGURATIONVAR. 'NBR_DAYS' => array(
    				'type'       => 'text', 
    				'title'      => $this->l('Number of days'),
                    'default'    => '',
                    'size'       => '30',
                    'validation' => 'isInt',
                    'desc'       => $this->l('Number of days during which the file will be accessed by customers after their order - (Set to 0 for unlimited access to download)')
    			),
                self::$CONFIGURATIONVAR. 'IS_SHAREABLE' => array(
    				'type'       => 'bool', 
    				'title'      => $this->l('Is shareable'),
                    'default'    => false,
                    'validation' => 'isBool',
                    'desc'       => $this->l('Yes if the downloadable product will be shareable, no otherwise.')
    			),
            ),
            array(
                'ATTRIBUTES' => array(
                    'title' => $this->l('Product price configs'),
                    'fieldset_style' => 'float:left;width: 47%;padding: 5px;margin: 5px;min-height:112px;',
                ),
                self::$CONFIGURATIONVAR. 'WHOLESALE_PRICE' => array(
    				'type'       => 'text', 
    				'title'      => $this->l('Purchase price HT'),
                    'default'    => '',
                    'size'       => '30',
                    'validation' => 'isFloat',
                    'desc'       => $this->l('The purchase price which you ordered the product')
    			),
                self::$CONFIGURATIONVAR. 'PRICE' => array(
    				'type'       => 'text', 
    				'title'      => $this->l('Sales price without tax'),
                    'default'    => '',
                    'size'       => '30',
                    'validation' => 'isFloat',
                    'desc'       => $this->l('The selling price of the product excluding tax')
    			),
                self::$CONFIGURATIONVAR. 'ID_TAX_RULES_GROUP' => array(
    				'type' => 'select', 
    				'title'=> $this->l('Tax rule'),
                    'default'=> '60',
                    'suffix'    => '<a href="index.php?controller=AdminTaxRulesGroup&amp;token='. $adminTaxRulesGroupToken .'&amp;addtax_rules_group" class="button" onclick="javascript:return confirm(\''. $this->l('You will lost current updates. Do you realy want to continue?') .'\');">
				                        <img title="'. $this->l('Create') .'" alt="'. $this->l('Create') .'" src="../img/admin/add.gif"> '. $this->l('Create') .'
				                     </a>',
                    'validation' => 'isInt',
                    'identifier'=> 'id_tax_rules_group',
                    'list'=> TaxRulesGroup::getTaxRulesGroupsForOptions(),
                    'desc' => ''
    			),
            ),
        );
	}
}
